$(function(){
	var $pagebar=$("#pagebar ul"),$pageInfo=$("#page_info"),
	pageNo=1,pageSize=10,count=0; type = 0,num=0,joinTable=$("[name= joinTable]").val(),flag=false;fileName="";//初始值
	number = "";
	/**---------------封装分页方法,count:总条目数,pageNo:当前页---------------*/
	function pageit(count){
		$pagebar.empty();
	//	$('input[type=number]').val(0);//初始化
		//首页和上一页
		if(pageNo==1){
			$pagebar.append("<li id='start_page' class='disabled'><a href='javascript:void(0);'>首页</a></li>");
			$pagebar.append("<li id='prev_page'  class='disabled'><a href='javascript:void(0);'>上一页</a></li>");
		}else{
			$pagebar.append("<li id='start_page' pn=1><a href='javascript:void(0);'>首页</a></li>");
			$pagebar.append("<li id='prev_page' pn="+(pageNo-1)+"><a href='javascript:void(0);'>上一页</a></li>");
		}
		var tmp=parseInt(count/10);
		var page_size=count%10==0?(tmp):(tmp+1);//总页数
		//中间数字页
		if(page_size<=5){
			//如果小于5页,则全部显示
			for(var i = 0; i <page_size; i++) {
				if(i==pageNo-1){
					$pagebar.append("<li pn="+(i+1)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
				}else{
					$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
				}
			}
		}else{
			//如果总页数大于5页,显示当前页和前后各两页以及两个...符号
			if(pageNo<3){
				for(var i = 0; i <5; i++) {
					if(i==pageNo-1){
						$pagebar.append("<li pn="+(i+1)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}else{
						$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}
				}
				$pagebar.append("<li class='disabled'><a href='javascript:void(0);'>...</a></li>");
			}else if(pageNo>page_size-2){
				$pagebar.append("<li class='disabled'><a href='javascript:void(0);'>...</a></li>");
				for(var i = page_size-5; i <page_size; i++) {
					if(i==pageNo-1){
						$pagebar.append("<li pn="+(i+1)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}else{
						$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}
				}
			}else{
				for (var i = pageNo-3; i <pageNo+2; i++) {
					if(i==pageNo-1){
						$pagebar.append("<li pn="+(pageNo)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}else{
						$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}
				}
			}
		}
		//下一页和末页
		if(page_size==0||pageNo==page_size){
			$pagebar.append("<li id='next_page' class='disabled'><a href='javascript:void(0);'>下一页</a></li>");
			$pagebar.append("<li id='end_page' class='disabled'><a href='javascript:void(0);'>末页</a></li>");
			$pagebar.append("<li class='disabled'> &nbsp;转到&nbsp;<input  type='number' min='0' max='"+page_size+"' style='width:70px;height:33px;clear:both;border:1px solid #eee;'>&nbsp;页</li>");
		}else{
			$pagebar.append("<li id='next_page' pn="+(pageNo+1)+"><a href='javascript:void(0);'>下一页</a></li>");
			$pagebar.append("<li id='end_page' pn="+page_size+"><a href='javascript:void(0);'>末页</a></li>");
			$pagebar.append("<li class='disabled'>&nbsp;转到&nbsp;<input  type='number' min='0' max='"+page_size+"' style='width:70px;height:33px;clear:both;border:1px solid #eee;'>&nbsp;页</li>");
		}
		
		//绑定事件
		$pagebar.find("li").not(".disabled").on("click",function(){
				pageNo=parseInt($(this).attr("pn"));//更新当前页码
				reloadList();
				$('#pagebar input[type=number]').val();
		});
		//分页信息条
		$pageInfo.find("span").each(function(i,k){
			if(i==0){
				if(page_size==0){
					$(k).html(0);
				}else{
					$(k).html((pageNo-1)*10+1);
				}
			}else if(i==1){
				$(k).html(count>pageNo*10?pageNo*10:count);
			}else{
				$(k).html(count);
			}
		});
	//页面跳转
	//$('input[type=number]').blur(function () {  
	//	goto($(this).val());
	//});  
	$('#pagebar input[type=number]').focus(function () {  
		$(document).keypress(function(event){
			if(event.keyCode == 13) {
				gotoPage($('#pagebar input[type=number]').val());
			}
		});
	});  
	
	//$('input[type=number]').val(number);
	function gotoPage(pagenumber){
		if(pagenumber>=0){
		    if(pagenumber>=page_size){
				pageNo=parseInt(page_size);// 更新当前页码为总页数
		    }else{
				pageNo=parseInt(pagenumber);// 更新当前页码
		    }
		    number = pageNo;
			reloadList();
	    }else{
	    	alert("请输入合法的页码");
	    }
	}
	return number;
	}

	/**------------------加载列表---------------------*/
	var noRecord= "<font style='color:red;font-weight:bold;font-size:13px;'>无</font>";
	var params={"pageNo":1,"pageSize":pageSize,"type":type,"num":num,"joinTable":joinTable,"flag":flag,"fileName":fileName};
	var authStatus = {false:"未认证",true:"已认证"};
	var delStatus = {false:"未删除 ",true:" 删除"};
	function reloadList(){
		params.pageNo=pageNo;
		params.pageSize=pageSize;
		params.type = type;
		params.num  = num;
		params.fileName=fileName;
		params.joinTable  = joinTable;
		params.flag  = flag;
		$.get(serverURL+"file/getFileListByCondition",params,function(data){
			try {
				if(data.code==0){
					data=data.data;
					$("#same_dict_table").find("tbody").empty();
					count=data.pagination.totalCount;
					if(count>0){
						var html="";
						$.each(data.list,function(i,k){
							html+="<tr>" +
							"<td id='pkc'><input type='checkbox' id='"+k.fileId+"' class='checkbox' col='"+i+"' ></td>" +
							"<td><a href=http://ow365.cn/?i=11328&furl="+k.path+" target='_blank' title='查看资料详情'>"+(k.name?k.name:noRecord)+"</a></td>"+ 
							"<td>"+k.introduction+"</td>"+ 
							"<td>"+k.downloadCount+"</td>"+ 
							"<td>"+k.collectionCount+"</td>"+ 
							"<td>"+k.commentCount+"</td>"+ 
							"</tr>";
						});
						$("#same_dict_table").find("tbody").append(html);
					}else{
						$("#same_dict_table").find("tbody").append("<tr><td colspan="+$("thead th").length+">无数据</td></tr>");
					}
					var pageNum =  pageit(count);//重绘工具栏
					$('#pagebar input[type=number]').val(pageNum);
					$('#num').val(num);
				}else if(data.code!=-1){
					alert("数据加载出错，请稍后重试");
				}
			} catch (e) {
				alert("数据加载出错，请稍后重试");
			}
		});
	}
	reloadList();
	//全选与取消全选
	$("#pkall").change(function(){
		if($(this).prop("checked")){
			$("#same_dict_table").find("#pkc :checkbox").prop("checked",true);
		}else{
			$("#same_dict_table").find("#pkc :checkbox").prop("checked",false);
		}
	});
	//获取选中的行
	function getSelectedCols(){
		return $("#same_dict_table").find("#pkc :checkbox").filter(":checked");
	}

	$(".dropdown-menu li a").click(function() {
		text = $(this).text();
		condition = $(this).attr("id") ; 
		$("#num").val("");
		$.get(serverURL+"file/getCountByCondition",{"type" : $(this).attr("id"),"joinTable" : joinTable},function(data){
			try {
				if(data.code==0){
						$("#btnGroupDrop1").html(text+"<span class='caret'></span>");
						$("#num").attr("placeholder",text+"最大值为"+data.data);
						$("#btnGroupDrop1").attr("condition",condition);
					}else{
						$("#btnGroupDrop1").html(text+"<span class='caret'></span>");
						$("#num").attr("placeholder",text+"最大值为0");
						$("#btnGroupDrop1").attr("condition",condition);
					}
			} catch (e) {
				alert("数据加载出错，请稍后重试");
			}
		});
	});
	/*****刷新****/
	$("#btn_refresh").click(function(){
		pageNo=1;
		flag  = false;
		reloadList();
	});
	
	/**我的文件***/
	$("#btn_myFile").click(function(){
		pageNo=1;
		flag = true;
		reloadList();
	})	
	
	//筛选
	$("#search").click(function(){
		 type = $("#btnGroupDrop1").attr("condition");
		//if(type == null){
		//	  alert("请选筛选条件");
		//	  return false;
		//}
		num = $("#num").val();
		fileName = $("#fileName").val();
		reloadList();
	});

    /** ************增加文件*************** */
   	 $("#btn_confirm_add").click(function() {
			 var $curCol = $("#same_dict_table").find("#pkc :checkbox").filter(":checked");
			if ($curCol.length == 0) {
				alert("请至少选择一条引用记录");
				return false;
			}
			var ids = "";
			$.each($curCol, function(i, k) {
				ids += $curCol[i].id + ",";
			})
			ids = ids.substring(0, ids.length - 1);	
			
			$.post(serverURL + "file/addSkillFile", {"skillId" : skillId,"fileIds":ids}, function(data) {
				if (data.code == 0) {
				  alert("添加成功");
				  pageNo = 1;
				  reloadList();
				  window.location.href="skill.htm";
				} else if (data.code != -1) {
					alert("添加失败,请稍后重试");
				}
			});		
	})
});